package com.wzdigit.wms.wms.rpc.transfer;

import com.wzdigit.framework.Shift;
import com.wzdigit.wms.basic.client.response.basic.RecommendedLocationResponse;
import com.wzdigit.wms.basic.client.response.basic.WarehouseApiResponse;
import com.wzdigit.wms.basic.service.code.WhsPermService;
import com.wzdigit.wms.common.entity.StatusCode;
import com.wzdigit.wms.wms.api.transfer.RecommendedLocationApi;
import com.wzdigit.wms.wms.dto.transfer.RecommendedLocationDto;
import com.wzdigit.wms.wms.service.basic.LocationService;
import com.wzdigit.wms.wms.service.basic.WarehouseService;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.BeanUtil;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 查询推荐库位实现类
 * Created by zyf on 2021/6/29 17:15
 */
@Service(version = "${dubbo.providers.wms.locinfo.api:1.0.0}")
@Slf4j
public class RecommendedLocationApiImplService implements RecommendedLocationApi {
    @Autowired
    private LocationService locationService;

    @Autowired
    private WarehouseService warehouseService;
    
    @Autowired
	private WhsPermService whsPremService;

    @Override
    public RecommendedLocationDto searchLocInfo(String locCode, String  loginName) {
        // 查询当前用户权限仓库
        List<WarehouseApiResponse> whList = whsPremService.getWarehousePerm(loginName);
        // 根据库位编码获取库存+库位信息
        RecommendedLocationResponse loc = locationService.selectWareHouseByLocCode(locCode);
        if (loc == null) {
            Shift.fatal(StatusCode.NOT_EXIST_LOC);
        }
        if (whList.stream().filter(c -> c.getWhsId().equals(loc.getWhsId())).count() == 0) {
            Shift.fatal("当前扫描库位不属于当前登录用户的权限仓库");
        }
        RecommendedLocationDto result=new RecommendedLocationDto();
        BeanUtil.copyNonNull(loc, result);
        return result;
    }
}
